package com.sgj.springcloud.alibaba.service.impl;

import com.sgj.springcloud.alibaba.dao.OrderDao;
import com.sgj.springcloud.alibaba.domain.Order;
import com.sgj.springcloud.alibaba.service.AccountService;
import com.sgj.springcloud.alibaba.service.OrderService;
import com.sgj.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 沈国纪
 * @date 2021/6/6 10:26
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

	@Resource
	private OrderDao orderDao;

	@Resource
	private StorageService storageService;

	@Resource
	private AccountService accountService;

	@Override
	@GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)
	public void create(Order order) {
		log.info("-----> 开始新建订单");
		orderDao.create(order);

		log.info("-----> 订单微服务开始调用库存，做扣减Count");
		storageService.decrease(order.getProductId(), order.getCount());
		log.info("-----> 订单微服务开始调用库存，做扣减 end");

		log.info("-----> 订单微服务开始调用账户，做扣减Money");
		accountService.decrease(order.getUserId(), order.getMoney());
		log.info("-----> 订单微服务开始调用账户，做扣减 end");

		// 修改订单状态，从 0 到 1 代表已经完成
		log.info("-----> 修改订单状态开始");
		orderDao.update(order.getUserId(), 0);
		log.info("-----> 修改订单状态结束");

		log.info("-----> 下订单结束了");

	}
}